/*
 * Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary information of iBOXSaaS Company of China.
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the contract agreement you entered into with iBOXSaaS inc.
 * 
 */

package com.iboxpay.open.gateway.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.util.StreamUtils;

import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * AccessLogUtils.java
 * 
 * @author xuxiao
 * @date 2018/06/15
 */
@Slf4j
public class LogUtils {
    
    public static String getResponseBody(RequestContext context) {
        try {
            InputStream is = context.getResponseDataStream();
            if (is != null) {
                String responseBody = StreamUtils.copyToString(is, Charset.forName("UTF-8"));
                context.setResponseDataStream(new ByteArrayInputStream(responseBody.getBytes(Charset.forName("UTF-8"))));
                return responseBody;
            }
        } catch (Exception e) {
            log.warn("[{}] [HTTP] - {} Exception:", context.getRequest().getRequestURI(), context.getRequest().getMethod(), e);
        }
        return null;
    }
    
}
